#include <accelerometer.h>
#include <display.h>
#include <h01.h>
#include <iap.h>
#include <rtc.h>
#include <usb.h>

extern pFunction Jump_To_Application;
extern uint32_t JumpAddress;
extern __IO uint8_t curfwstatus;
extern __IO uint8_t prefwstatus;

//------------------------------------------------------------------------------

uint	steps;
uint	timeOfs;
uint	timer;
uint	flashID=0;

ushort	error;
ushort	flags;

ubyte	status=STATUS_BOOTING;
ubyte	flag_uploaddat=0;
ubyte	usbplug=0;

static void silent(ubyte stt){
	displayOff();
	while(status==stt){
		__WFI();
		SystemInit();
		RCC_HCLKConfig(RCC_SYSCLK_Div4);
		ubyte s=getStatus();
		status=s==STATUS_TAP?STATUS_PEDOMETER:s;
	}
}

//------------------------------------------------------------------------------

void main(void){
	init(RCC_SYSCLK_Div2);
	usbOn();
	initAccelerometer();
	waitRTC();
	while(1){
		switch(status){
		case STATUS_BOOTING:
			status=STATUS_CLOCK;
			break;
		case STATUS_CHARGING:
			status=STATUS_SILENT;
			break;
		case STATUS_CLOCK:
			clock();
			break;
		case STATUS_PEDOMETER:
			pedometer();
			break;
		case STATUS_SILENT:
		case STATUS_STANDBY:
			silent(status);
			break;
		default:
			status=getStatus();
		}
	}
}